package com.nl.sq;

import java.util.Stack;

public class StackPopOrder {

	public static boolean isPopOrder(int[] seq,int[]popOrder){
		if(seq ==null || popOrder == null ||seq.length != popOrder.length){
			return false;
		}
		
		Stack<Integer> seqStack =new Stack<Integer>(); 
		int j=0;
		for(int i=0;i<popOrder.length;i++){
			//和栈顶元素对比，如果是一样的，弹出元素，如果不一样，则继续压栈直到相同，如果都压入栈中，还没有相等的元素，则不满足．
			while(seqStack.isEmpty()||popOrder[i]!=seqStack.peek()){
				if(j==seq.length){
					break;
				}
				seqStack.push(seq[j++]);
			}
			if(seqStack.peek()!=popOrder[i]){
				break;
			}
			seqStack.pop();
		}
		if(seqStack.isEmpty() && j==seq.length){
			return true;
		}

		return false;
	}
	public static void main(String[] args) {
		System.out.println(isPopOrder(new int[]{1,2,3,4,5}, new int[]{4,5,3,2,1}));
	}
}
